
local cjson = require("cjson")
math.randomseed(os.time())
cjson.encode_empty_table_as_object(false)

local req = {}

math.randomseed(os.time())
local counter = 1
local reqs = 0
local threads = {}

wrk.model = {
   url = "http://smc-service.i.seller.test.shopee.io/api/v1/crm/promotion-voucher-v1",
   timeout = 30,
   phase = {
      {connections="100", duration="5s", threads=1},
      {connections="200", duration="10s", threads=1},
      {connections="400", duration="10s", threads=1},
      {connections="800", duration="1m", threads=1},
      {connections="1k", duration="1m", threads=1},
      {connections="2k", duration="1m", threads=1},
      {connections="3k", duration="1m", threads=1},
      {connections="4k", duration="1m", threads=1},
   }
}

wrk.method = "POST"
wrk.headers["smc-token"] = "crmtest"
wrk.headers["Content-Type"] = "application/json"


function request()
   local memory_increased = collectgarbage("count") 
   print("request:".. memory_increased)
   local shop_id = math.random(106793, 906793)
   local activity_id = string.gsub(wrk.uuid(2), '-', '')
   local body = {
      params = {
         shop_id = shop_id,
         activity_id = activity_id
      }
   }
   local s = wrk.format(nil, nil, nil, cjson.encode(body))
   -- print("s ", s)
   return s
end


function response(status, c)
   local memory_increased = collectgarbage("count") 
   
   if not string.match(c.response_body, "total") then
      print("req==> ".. c.request)
      print("resp==>".. c.response_body)
      c:set("bizeError", 1)
   end

   return nil, 500
end

